package cn.edu.henu.team.fragments;

import android.app.ProgressDialog;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.edu.henu.team.R;
import cn.edu.henu.team.base.BaseFragment;
import cn.edu.henu.team.bean.examination;
import cn.edu.henu.team.table.examinationsUtils;
import cn.edu.henu.team.table.examinations;
import cn.edu.henu.team.ui_test.DescribeActivity;
import cn.edu.henu.team.ui_test.ExamActivity;

public class fragment2_test extends BaseFragment{
    private TextView start_test;
    private TextView skip_test;

    public fragment2_test() {}

    public static fragment2_test newInstance() {
        fragment2_test fragment = new fragment2_test();
        return fragment;
    }

    @Override
    protected int initLayout() {
        return R.layout.fragment_2_test;
    }

    @Override
    protected void initView() {
        start_test = mRootView.findViewById(R.id.tv_start_test);
        skip_test = mRootView.findViewById(R.id.tv_skip_test);
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void clickEvents() {
        skip_test.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(getActivity(), DescribeActivity.class));
            }
        });

        start_test.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                final ProgressDialog progressDialog = new ProgressDialog(getContext());
                progressDialog.setCancelable(true);
                progressDialog.setMessage("正在加载数据中,请稍后...");
                progressDialog.show();
                BmobQuery<examination> mQuery = new BmobQuery("examinations");
                mQuery.order("question");
                mQuery.findObjects(new FindListener<examination>() {
                    @Override
                    public void done(List<examination> list, BmobException e) {
                        if (e == null) {
                            if (list.size() != 0) {
                                examinationsUtils.deleteAll();
                                for (examination t : list) {
                                    examinations ex = new examinations();
                                    ex.setQuestion(t.getQuestion());
                                    ex.setItemA(t.getItemA());
                                    ex.setItemB(t.getItemB());
                                    examinationsUtils.insert(ex);
                                }
                                progressDialog.dismiss();
                                startActivity(new Intent(getActivity(), ExamActivity.class));
                            } else
                                progressDialog.dismiss();
//                                Toast.makeText(getContext(), "服务器数据为空~", Toast.LENGTH_SHORT).show();
                        } else {
                            progressDialog.dismiss();
                            Log.e("失败信息  ", e.toString());
                        }
                    }
                });
            }
        });
    }
}